package com.tao.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.net.httpserver.Authenticator;
import com.tao.factorys.AnswererFactory;
import com.tao.pojo.Answerer;
import com.tao.service.AnswererService;
import com.tao.service.QiniuServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @Author: alubus
 * @Date: 2021/11/9 17:42
 * @Package: com.tao.controller
 */
@RestController
@RequestMapping("/answerer")
public class AnserController {

    Logger logger= LoggerFactory.getLogger(getClass());

    @Autowired
    AnswererService answererService;

    @Autowired
    AnswererFactory answererFactory;

    @RequestMapping("/selectAll")
    public List<Answerer> selectAll(){
        List<Answerer> list = answererService.selectAll();
        return list;
    }

    @Autowired
    QiniuServiceImpl qiniuService;

    // localhost:8001/answerer/addAs
//    增加123 号的回答个数
    @RequestMapping("/addAs")
    public int addAs(){
        int i = answererService.addAnswerSum(123L);
        return i;
    }

    @RequestMapping("/addAnswerer")
    public String add1(){
        Answerer answerer = answererFactory.getAnswerer();
        boolean b = answererService.addAnswerer(answerer);
        if (b){
            return "add Answerer success";
        }
        return "add Answerer false";
    }

    /*
    * 分页 查询
    * 起始页码 1  不是0
    * */
//new Page<>(2, 2)
    @RequestMapping("/getByPage")
    public List<Answerer> getAnsById(long current,long size){
        List<Answerer> answerers = answererService.selectPageVo(new Page<>(current, size));

//        logger.info("getByPage:"+page.getCurrent()+"----"+page.getPages());
//        List<Answerer> answerers = answererService.selectPageVo(page);
        return answerers;
    }


    @RequestMapping("/getAllCount")
    public int getAllCount(){

        int count = answererService.getCount();

//        logger.info("getByPage:"+page.getCurrent()+"----"+page.getPages());
//        List<Answerer> answerers = answererService.selectPageVo(page);
        return count;
    }

//    @RequestMapping("/addAnswerer")
//    public String getAnsById(){
//        Answerer answerer = answererService.selectByid();
//
//        return "添加失败";
//    }


    @PostMapping("/addHead")
    private String addHead(MultipartFile file){
        String fileName="syt/images/head/";
        try {
            fileName=qiniuService.saveImage(file);

        } catch (IOException e) {
            System.out.println("上传失败");
            return fileName;
        }
//        syt/images/head/

        return qiniuService.getQnyUrl(fileName);
    }

//    @RequestMapping("/getHead")
//    private String getHead(String head){
//        try {
//            return qiniuService.getQnyUrl(head);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return null;
//        }
//    }


//    @RequestMapping("/getHead")
//    private String getHead(String head){
//        try {
//            return qiniuService.getQnyUrl(head);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return null;
//        }
//    }






//
//    /七牛云 查询（就是多了个查询 网上的 url）
//
//    @RequestMapping("/selectAllPic")
//    public String selectAllPic(){
//
//        List<Pic> pics = picService.selectAll();
//        System.out.println("通过七牛云调用以下图片： ---------------------------------");
//        for (Pic pic : pics) {
////            设置当前七牛云的访问路径
//            pic.setPicurl(qiniuServiceImpl.getQnyUrl(pic.getPicname()));
//        }
//        System.out.println("图片end：---------------------------------");
//        return JSON.toJSONString(pics);
//    }
//
//    //七牛云删除
//    @RequestMapping("/delPic/{id}")
//    public  String delPic(@PathVariable int id){
//        JSONObject o =new JSONObject();
//        Pic pic = picService.selectByPrimaryKey(id);
//
//
//        //删除文件
//        String path="images/gallery/"+pic.getPicname();
//        String s = qiniuServiceImpl.delImage(path);
//
//        o.put("msg",s);
//        picService.deleteByPrimaryKey(id);
//
//        return JSON.toJSONString(o);
//    }
//

}
